﻿$(function () {

    //Get the categories and load them into the select element
    $.ajax({
        url: "/api/category/listcategories",
        type: "GET",
        contentType: "application/json;charset=utf-8"
    }).then(function (categories) {
        if (categories) {
            $('#categories').append('<option>' + Globalize.localize("SelectItem") + '</option>');
            for (var i in categories) {
                $('#categories').append('<option value="' + categories[i].ProductCategoryId + '">' + categories[i].CategoryName + '</option>');
            }
        }
    });

    //Get the brands and load them into the select element
    $.ajax({
        url: "/api/brand/listbrands",
        type: "GET",
        contentType: "application/json;charset=utf-8"
    }).then(function (brands) {
        if (brands) {
            $('#brands').append('<option>' + Globalize.localize("SelectItem") + '</option>');
            for (var i in brands) {
                $('#brands').append('<option value="' + brands[i].BrandId + '">' + brands[i].BrandName + '</option>');
            }
        }
    });

    //Set the selected category value to the hidden input element
    $('#categories').change(function () {
        var selected = $('option:selected', this).val();

        if (selected === Globalize.localize("SelectItem")) {
            $('#ProductCategoryId').val('');
        } else {
            $('#ProductCategoryId').val(selected);
        }
    });

    //Set the selected brand value to the hidden input element
    $('#brands').change(function () {
        var selected = $('option:selected', this).val();

        if (selected === Globalize.localize("SelectItem")) {
            $('#BrandId').val('');
        } else {
            $('#BrandId').val(selected);
        }
    });
});